/*
 Copyright 2012 by Trypha (RK1)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package jaotwinker;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Grayson
 */
public class CleanParser extends DefaultHandler {
    public List items;
    private Item tempItem;
    private Skill tempSkill;
    private Requirement tempReq;
    private String tempVal;
    MainWindow window;
    
    public int numChars = 0;
    
    public void parse(String filename, MainWindow window) {
        this.window=window;
        //System.out.println("Parsing " + filename + "...");
        items = new ArrayList();
        
        //list = new ArrayList();
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            SAXParser sp = spf.newSAXParser();
            sp.parse(filename, this);
        }catch(SAXException se) {
                se.printStackTrace();
        }catch(ParserConfigurationException pce) {
                pce.printStackTrace();
        } catch(IOException io) {
                io.printStackTrace();
        }
        //while(!task.done) {}
    }
    
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equalsIgnoreCase("Item")) {
            if(tempItem==null) { System.out.println("WTF null item"); return; }
            tempItem.sortSlots();
            items.add(tempItem); tempItem = null;
        }
        else if(qName.equalsIgnoreCase("Skill")) { 
            if(tempItem==null || tempSkill==null) { System.out.println("WTF @Skill"); return; }
            tempItem.Skills.add(tempSkill); tempSkill=null;
        }
        else if(qName.equalsIgnoreCase("Requirement")) { 
            if(tempItem==null || tempReq==null) { System.out.println("WTF @Req"); return; }
            tempItem.Requirements.add(tempReq); tempReq = null;
        }
    }
    
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        Random generator = new Random();
        window.jProgressBar1.setValue(generator.nextInt(99));
        if(qName.equalsIgnoreCase("Item")) { addItem(attributes); }
        else if(qName.equalsIgnoreCase("Skill")) { addSkill(attributes); }
        else if(qName.equalsIgnoreCase("Requirement")) { addRequirement(attributes); }
    }
    
    public void addItem(Attributes attributes) {
        tempItem = new Item();
        int id = Integer.parseInt(attributes.getValue("AOID"));
        String name = attributes.getValue("Name");
        int ql = Integer.parseInt(attributes.getValue("QL"));
        int icon = Integer.parseInt(attributes.getValue("Icon"));
        String type = attributes.getValue("ItemType");
        String slot = attributes.getValue("Slot");
        tempItem.set(id, name, ql, type);
        tempItem.icon = icon; tempItem.slot = slot;
        if(tempItem.Name==null) {
            System.out.println("Item " + tempItem.AOID + " has no name.");
        }
    }
    
    public void addSkill(Attributes attr) {
        if(tempItem==null) {
            System.out.println("wtf: addskill");
            return;
        }
        tempSkill = new Skill();
        tempSkill.name = attr.getValue("Name");
        tempSkill.value = attr.getValue("Value");
    }
    
    public void addRequirement(Attributes attr) {
        if(tempItem==null || attr==null) {
            System.out.println("wtf: addreq");
            return;
        }
        tempReq = new Requirement();
        tempReq.name = attr.getValue("Name");
        tempReq.operator = attr.getValue("Operator");
        tempReq.value = attr.getValue("Value");
        tempReq.childop = attr.getValue("ChildOp");
        tempItem.Requirements.add(tempReq);
    }
    
    public void characters(char[] ch, int start, int length) throws SAXException {
        //tempVal = new String(ch,start,length);
        numChars+=length;
    }
}
